<?php
namespace XtwsAdmin\Controller;
use Think\Controller;

class CategoryController extends CommonController {

    // 分类列表
    public function index(){
        $cate = M('cate');
        $where = "";
        if ($_GET['keyword'] != '') {
            $where = "title like '%" . $_GET['keyword'] . "%'";
            $this->assign('keyword', $_GET['keyword']);// 赋值分页输出
        }
        $list = $cate->where($where)->select();
        if ($_GET['keyword'] == ''){
            $list = list_to_tree($list); //转换为分类树
        }
        $this -> assign('list',$list);
        //加载视图
        $this->display();
    }//end fun


    // 添加修改分类
    public function addCate(){
        $cate = M('cate');
        $keywords = M('keywords');
        $id  = intval(I('id'));
        if(IS_POST){
            $data = I('post.') ;
//            dump($data);
            // 更新所有绑定关键词的状态
            foreach ($data['keyid'] as $k=>$v){
                $keywords->where('id='.$v)->setField('is_bind',1);
                $ktitle[] = $keywords->where('id='.$v)->getField('keyword');
            }
            unset($data['keyid']);
            $data['keywords'] = implode(",", $ktitle);
            if($id){//编辑
                // 拼接新增的关键词
                $old_keyword = $cate->where(array('id'=>$id))->getField('keywords');
                if($old_keyword!=''){
                    $data['keywords'] = $old_keyword.','.$data['keywords'];
                }
                $res = $cate -> save($data);
            }else{//添加
                $data['add_time'] = time();
                $res = $cate -> add($data);
            }// end if
            if($res){
//                $this -> success('提交成功','/XtwsAdmin/Category/index');
                $this -> success('提交成功');
            }else{
                $this -> error('提交失败');
            }// end if
        }else{
            //查询所有分类
            $cate_list = $cate->field('id,pid,title')->select();
            $cate_list = list_to_tree($cate_list); //转换为分类树
            if($id){
                $cateInfo = $cate->where(array('id'=>$id))->find();
                dump($cateInfo);
            }else{
                $cateInfo = 1;
            }
            // 查询未绑定的关键词
            $cate_keywords = $keywords->where('is_bind=0')->limit(500)->select();
            $this -> assign('cateInfo',$cateInfo);
            $this -> assign('cate_list',$cate_list);
            $this -> assign('cate_keywords',$cate_keywords);
            $this -> display();
        }// end if

    }// end fun

    // 店铺名称搜索
    public function searchKwords(){
        $store_key = I('store_key');    //搜索关键词
        if($store_key){
            $where = "is_bind = 0 AND keyword like '%".$store_key."%'";
            $keywords = M('keywords');
            $list = $keywords->where($where)->field('keyword,id')->select();
            if($list!=''){
                $this->ajaxReturn($list);
            }
        }
    }// end fun
    // 删除分类
    public function del_cate(){
        $id  = intval(I('id'));
        if($id){
            $cate = M('cate');
            $data['id'] = $id;
            $res = $cate->delete($id);
            if($res){
                $this->success('删除成功');
            }else{
                $this->error('出错啦，请检查');
            }
        }else{
            $this->error('请先选择数据');
        }

    }//end fun


}